package main

import (
	"fmt"
	"github.com/armed/mkdirp"
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
	ginSwagger "github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"
	"net/http"
	"os"
	"path/filepath"
	"shimmer/neusoft_club_backend/controllers"
	_ "shimmer/neusoft_club_backend/docs"
	"shimmer/neusoft_club_backend/services"
	"shimmer/neusoft_club_backend/services/assets_service"
	"shimmer/neusoft_club_backend/services/db_sync"
)

// @title 社团纳新系统
// @version 1.0
// @description club

// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io

func main() {
	//fmt.Println(services.ConfigServiceInst纳新)
	fmt.Printf("config loaded : %+v\n", services.ConfigServiceInstance)
	db_sync.SyncDB()
	r := gin.New()
	// gin.SetMode(gin.ReleaseMode)
	store := cookie.NewStore([]byte(services.ConfigServiceInstance.Cookie.Key))
	store.Options(sessions.Options{HttpOnly: true, Path: "/", MaxAge: 86400 * 30})

	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	uploadsAbsPath, _ := filepath.Abs(assets_service.FileUploadBasePath)
	_ = mkdirp.Mk(uploadsAbsPath, os.ModeDir)

	r.StaticFS(assets_service.PublicUploadBasePath, http.Dir("./uploads"))
	r.Use(sessions.Sessions("club_session", store))
	r.Use(gin.Logger()).Use(gin.CustomRecovery(func(c *gin.Context, recovered interface{}) {
		if err, ok := recovered.(string); ok {
			c.JSON(http.StatusInternalServerError, gin.H{
				"message": err,
			})
		}
		c.AbortWithStatus(http.StatusInternalServerError)
	}))
	{
		apiGroup := r.Group("/api")
		controllers.ConfigureControllers(apiGroup)
	}

	err := r.Run(fmt.Sprintf("%s:%d", services.ConfigServiceInstance.Web.Address, services.ConfigServiceInstance.Web.Port)) // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
	panic(err)
}
